package com.sina.shop.dao.order;

import com.sina.shop.dao.ShopDataSourceDao;
import com.sina.shop.domain.order.Order;
import com.sina.shop.domain.order.Status;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-02-05-17:20
 */
@Repository("orderDao")
public class OrderDaoImpl extends ShopDataSourceDao implements OrderDao {

    @Override
    public List<Order> getOrderListByAdminId(Long adminId, Status status) {

        String hql = "from Order o where o.card.admin.id= :adminId and o.status= :status order by o.createTime desc";
        return list(hql, new String[]{"adminId", "status"}, new Object[]{adminId, status}, -1, -1);

    }

    @Override
    public Integer deleteOrder(Long id) {
        String hql = "delete from Order o where o.id = :id";
        return template.executeWithNativeSession(session -> {
            Query query = session.createQuery(hql);
            query.setParameter("id", id);

            return query.executeUpdate();
        });
    }

    @Override
    public Integer updateOrderStatus(Long id, Status status) {
        String hql = "update Order o set o.status = :status where o.id = :id";
        return template.executeWithNativeSession(session -> {
            Query query = session.createQuery(hql);
            query.setParameter("id", id);
            query.setParameter("status", status);
            return query.executeUpdate();
        });
    }
}
